package com.shoujifeng.snowmusic.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import com.shoujifeng.snowmusic.player.R;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListAdapter extends  BaseAdapter {

	private ArrayList<HashMap<String, String>> arrayList;
	private String[] mKeys;
	private int[] mIds;
	private LayoutInflater mLayoutInflater;
	private int mListlayout;
	private boolean mChange = false;

	public ListAdapter(Context context,
			ArrayList<HashMap<String, String>> array, String[] keys, int[] ids,int layout) {

		setDatas(context, array, keys, ids, layout);
	}

	
	private void setDatas(Context context, ArrayList<HashMap<String, String>> array, String[] keys, int[] ids,
			 int layout){
		
		mLayoutInflater = LayoutInflater.from(context);
		
		arrayList = array;
		mKeys = keys;
		mIds = ids;
		mListlayout = layout;
		
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arrayList.size();
	}

	@Override
	public HashMap<String, String> getItem(int pos) {
		// TODO Auto-generated method stub
		return arrayList.get(pos);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (convertView == null || mChange) {
			convertView = mLayoutInflater.inflate(mListlayout, null);
		}

		HashMap<String, String> map = getItem(position);

		for (int i = 0; i < mKeys.length; i++) {
			TextView text = (TextView) convertView.findViewById(mIds[i]);
			text.setText(map.get(mKeys[i]).toString());
		}

		return convertView;
	}

	// �޸�Adapter����
	public void setAdapter(ArrayList<HashMap<String, String>> array,
			String[] keys, int[] ids, int layout) {

		arrayList = array;
		mKeys = keys;
		mIds = ids;
		mListlayout = layout;
		mChange = true;
	}
}
